import env_default from './env.json';

export default (() => {
  try {
    const globalEnvKey = '__ENV_GLOBAL';

    if (typeof process === 'object' && typeof process.env === 'object' && process.env) {
      window[globalEnvKey] = Object.assign({}, env_default, process.env);
      return {
        ...window[globalEnvKey],
        BASE_URL: window[globalEnvKey].BASE_URL,
        MODE: window[globalEnvKey].NODE_ENV,
        PROD: window[globalEnvKey].NODE_ENV === 'production',
        DEV: window[globalEnvKey].NODE_ENV === 'development',
        MICRO_XW_ENV_KEY: '__MICRO_XW_ENV__',
        MICRO_XW_ENV: () => window.name === '__MICRO_XW_ENV__',
        get: (key) => window[globalEnvKey][key],
      };
    }
    if (typeof import.meta === 'object' && typeof import.meta.env === 'object' && import.meta.env) {
      window[globalEnvKey] = Object.assign({}, env_default, import.meta.env);
      return {
        ...window[globalEnvKey],
        BASE_URL: window[globalEnvKey].BASE_URL,
        MODE: window[globalEnvKey].MODE,
        PROD: window[globalEnvKey].PROD,
        DEV: window[globalEnvKey].DEV,
        MICRO_XW_ENV_KEY: '__MICRO_XW_ENV__',
        MICRO_XW_ENV: () => window.name === '__MICRO_XW_ENV__',
        get: (key) => window[globalEnvKey][key],
      };
    }
    return { get: () => {} };
  } catch (e) {
    return { get: () => {} };
  }
})();
